package com.isoft.c2team3service2.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.isoft.c2team3service2.entity.User;
import com.isoft.c2team3service2.entity.UserDTO;
import com.isoft.c2team3service2.entity.UserFollow;
import com.isoft.c2team3service2.mapper.UserFollowMapper;
import com.isoft.c2team3service2.mapper.UserMapper;
import com.isoft.c2team3service2.service.UserFollowService;
import com.isoft.c2team3service2.util.UserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserFollowServiceImpl extends ServiceImpl<UserFollowMapper, UserFollow> implements UserFollowService {

    @Autowired
    private UserFollowMapper userFollowMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    @Transactional
    public boolean followUser(Integer followedId) {
        UserDTO currentUser = UserHolder.getUser();
        if (currentUser.getId().equals(followedId)) {
            return false; // 不能关注自己
        }
        // 检查是否已关注
        if (isFollowed(followedId)) {
            return true; // 已关注，直接返回成功
        }
        // 新增关注关系
        UserFollow follow = new UserFollow();
        follow.setFollowerId(currentUser.getId());
        follow.setFollowedId(followedId);
        follow.setCreateTime(LocalDateTime.now());
        return save(follow);
    }

    @Override
    @Transactional
    public boolean unfollowUser(Integer followedId) {
        UserDTO currentUser = UserHolder.getUser();
        LambdaQueryWrapper<UserFollow> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserFollow::getFollowerId, currentUser.getId())
                .eq(UserFollow::getFollowedId, followedId);
        return remove(wrapper);
    }

    @Override
    public boolean isFollowed(Integer followedId) {
        UserDTO currentUser = UserHolder.getUser();
        return userFollowMapper.isFollowed(currentUser.getId(), followedId);
    }

    @Override
    public List<UserDTO> getFollowedList() {
        UserDTO currentUser = UserHolder.getUser();
        // 查询关注的用户ID列表
        List<Integer> followedIds = userFollowMapper.selectFollowedIds(currentUser.getId());
        // 转换为UserDTO（含昵称/头像）
        return followedIds.stream()
                .map(id -> userMapper.selectUserDTOById(id))
                .collect(Collectors.toList());
    }

    @Override
    public List<UserDTO> getFollowerList() {
//        UserDTO currentUser = UserHolder.getUser();
//        // 查询粉丝ID列表
//        List<Integer> followerIds = userFollowMapper.selectFollowerIds(currentUser.getId());
//        // 转换为UserDTO（含昵称/头像）
//        return followerIds.stream()
//                .map(id -> userMapper.selectUserDTOById(id))
//                .collect(Collectors.toList());
        return null;
    }
}